package com.qingke.memo.dao;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.qingke.memo.pojo.CustomTag;
import com.qingke.memo.pojo.Days;
import com.qingke.memo.pojo.Memo;
import com.qingke.memo.pojo.MemoCustomTag;

public class MemoDAO {
	private String resource = "mybatis_conf.xml";
	private InputStream is=null;
	private SqlSessionFactory sessionFactory = null;
	private SqlSession session = null;
	
	public MemoDAO(){
		
	}
	
	{
		is=MemoDAO.class.getClassLoader().getResourceAsStream(resource);
		sessionFactory = new SqlSessionFactoryBuilder().build(is);
		session = sessionFactory.openSession();
	}
	
	public boolean insertMemo(Memo memo){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		int count=mm.insert(memo);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean updateMemo(Memo memo){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		int count=mm.updateByPrimaryKey(memo);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean deleteMemo(Memo memo){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		int count=mm.deleteByPrimaryKey(memo.getId());
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean isMemoExistById(int id){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		Memo memo=mm.selectByPrimaryKey(id);
		if(memo==null)
			return false;
		return true;
	}
	
	public Memo selectMemoById(int id){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		return mm.selectByPrimaryKey(id);
	}
	
	public List<Memo> selectAllMemos(){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		return mm.selectAllMemos();
	}
	
	public boolean insertCustomTag(CustomTag customTag){
		CustomTagMapper ctm=session.getMapper(CustomTagMapper.class);
		int count=ctm.insert(customTag);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean updateCustomTag(CustomTag customTag){
		CustomTagMapper ctm=session.getMapper(CustomTagMapper.class);
		int count=ctm.updateByPrimaryKey(customTag);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean deleteCustomTag(CustomTag customTag){
		CustomTagMapper ctm=session.getMapper(CustomTagMapper.class);
		int count=ctm.deleteByPrimaryKey(customTag.getId());
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean isCustomTagExistById(int id){
		CustomTagMapper ctm=session.getMapper(CustomTagMapper.class);
		CustomTag customTag=ctm.selectByPrimaryKey(id);
		if(customTag==null)
			return false;
		return true;
	}
	
	public boolean isCustomTagExistByCode(String code){
		CustomTagMapper ctm=session.getMapper(CustomTagMapper.class);
		CustomTag customTag=ctm.selectByCode(code);
		if(customTag==null)
			return false;
		return true;
	}
	
	public CustomTag selectCustomTagById(int id){
		CustomTagMapper ctm=session.getMapper(CustomTagMapper.class);
		return ctm.selectByPrimaryKey(id);
	}
	
	public CustomTag selectCustomTagByCode(String code){
		CustomTagMapper ctm=session.getMapper(CustomTagMapper.class);
		return ctm.selectByCode(code);
	}
	
	public boolean isMemoCustomTagExistByMemo(MemoCustomTag memoCustomTag){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		List<MemoCustomTag> list=mctm.selectMemoCustomTagByMemo(memoCustomTag);
		if(list.size()==0)
			return false;
		return true;
	}
	
	public boolean isMemoCustomTagExistByCustomTag(MemoCustomTag memoCustomTag){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		List<MemoCustomTag> list=mctm.selectMemoCustomTagByCustomTag(memoCustomTag);
		if(list.size()==0)
			return false;
		return true;
	}
	
	public MemoCustomTag selectMemoCustomTagByMemoAndCustomTag(MemoCustomTag memoCustomTag){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		return mctm.selectMemoCustomTagByMemoAndCustomTag(memoCustomTag);
	}
	
	public List<MemoCustomTag> selectAllMemoCustomTags(){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		return mctm.selectAllMemoCustomTags();
	}
	
	public boolean insertMemoCustomTag(MemoCustomTag memoCustomTag){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		int count=mctm.insert(memoCustomTag);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean updateMemoCustomTag(MemoCustomTag memoCustomTag){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		int count=mctm.updateByPrimaryKey(memoCustomTag);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean deleteMemoCustomTag(MemoCustomTag memoCustomTag){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		int count=mctm.deleteByPrimaryKey(memoCustomTag.getId());
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public List<CustomTag> selectCustomTagsByMemo(Memo memo){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		List<MemoCustomTag> listMCT=mctm.selectMemoCustomTagsByMemo(memo);
		List<CustomTag> listCT=new ArrayList<>();
		for(MemoCustomTag mct:listMCT){
			listCT.add(mct.getCustomTag());
		}
		return listCT;
	}
	
	public List<Memo> selectMemosByTitle(Memo memo){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		return mm.selectByTitle(memo);
	}
	
	public List<Memo> selectMemosByContentKey(Memo memo){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		return mm.selectByContentKey(memo);
	}
	
	public List<Memo> selectMemosByCreateTime(Days days){
		MemoMapper mm=session.getMapper(MemoMapper.class);
		return mm.selectByDatetime(days);
	}
	
	public MemoCustomTag selectMemoCustomTagById(int id){
		MemoCustomTagMapper mctm=session.getMapper(MemoCustomTagMapper.class);
		return mctm.selectByPrimaryKey(id);
	}
}
